package SocketDemo.UDP;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author:周鹏
 * @date: 2020/11/9 17:13
 * @description:
 */
public class UdpClient {
    public static void main(String[] args) throws Exception{
//        //建立Scoket
//        DatagramSocket datagramSocket=new DatagramSocket();
//        String msg="事实上的撒的撒";
//        //建包
//        DatagramPacket datagramPacket=new DatagramPacket( msg.getBytes(),0,msg.getBytes().length, InetAddress.getByName("127.0.0.1"),9090);
//
//        //发送包
//        datagramSocket.send(datagramPacket);
//        //关闭流
//        datagramSocket.close();
        DatagramSocket datagramSocket=new DatagramSocket();
        //控制台读取数据
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
   while(true){
       //逐行读取数据
       String data=bufferedReader.readLine();
       byte[] buffer=data.getBytes();
       DatagramPacket datagramPacket=new DatagramPacket( buffer,0,buffer.length, InetAddress.getByName("127.0.0.1"),9090);
       datagramSocket.send(datagramPacket);
       if ("bye".equals(data)){
           break;

       }
   }


        //关闭流
        bufferedReader.close();
        datagramSocket.close();
    }
}
